<?php
session_start();
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserDaoImpl
 *
 * @author krisada.thiangtham
 */
//require '../../model-db-connection/config.php';
require '../com.swiss.mooh.utils/ArrayList.php';
require '../com.swiss.mooh.dao/UserDao.php';

class UserDaoImpl implements UserDao {

    public function __construct() {
        
    }

    public function verifyAndGetPassword($newPassword) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("UPDATE SWISS_USERS SET PASSWORD = ? WHERE USERNAME LIKE ?");
        $stmt->bind_param('ss', $newPassword, $_SESSION['username']);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function verifyUserByUsernameAndPassword($userTO) {
//        require '../com.swiss.mooh.model/User.php';
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_USERS WHERE username LIKE ? AND password LIKE ?");
        $stmt->bind_param('ss', $userTO->getUsername(), md5($userTO->getPassword()));
        if ($stmt->execute()) {
            $meta = $stmt->result_metadata();
            while ($field = $meta->fetch_field()) {
                $parameters[] = &$row[$field->name];
            }
            call_user_func_array(array($stmt, 'bind_result'), $parameters);
            while ($stmt->fetch()) {
                foreach ($row as $key => $val) {
                    $x[$key] = $val;
                }
                $userVO = new User();
                $userVO->setUserID($x['id']);
                $userVO->setUsername($x['username']);
                $userVO->setPassword($x['password']);
                $userVO->setPermissionId($x['permission_id']);
                $detailList->Add($userVO);
                $this->initiateTimeOut($userVO);
            }
            $stmt->close();
            return $detailList;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function initiateTimeOut($userVO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $_SESSION['expire'] = time() + (60 * $conf['applicationTimeOut']);
        $_SESSION['username'] = $userVO->getUsername();
        $_SESSION['permission_id'] = $userVO->getPermissionId();
    }

    public function updateNewTimeout($username, $permission_id) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $_SESSION['expire'] = time() + (60 * $conf['applicationTimeOut']);
        $_SESSION['username'] = $username;
        $_SESSION['permission_id'] = $permission_id;
        return 'SUCCESS';
    }

    public function checkTimeOut($currentTime) {
        if ($currentTime > $_SESSION['expire']) {
            session_destroy();
            return 'TIMEOUT';
        } else {
            return 'NOT_TIMEOUT';
        }
    }

}
